<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_thiessenAnalyst"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_thiessenAnalyst"></h5></div>
    <div class='panel-body content'>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_datasetThiessen"
               onclick="bufferAnalystProcess()"/>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_geoThiessen"
               onclick="geometryAnalystProcess()"/>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_removeResult"
               onclick="removeResult()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var host = window.isLocal ? window.server : "https://iserver.supermap.io";
    var map, local, baseLayer, resultLayer, spatialAnalystURL,
        url1 = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        url2 = host + "/iserver/services/spatialanalyst-changchun/restjsr/spatialanalyst";

    init();

    function init() {
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })], units: "m"
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));
        map.allOverlays = true;
        baseLayer = new SuperMap.Layer.TiledDynamicRESTLayer("Changchun", url1, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        baseLayer.events.on({"layerInitialized": addLayer});
        resultLayer = new SuperMap.Layer.Vector("分析结果");

    }

    function addLayer() {
        map.addLayers([baseLayer, resultLayer]);
        map.setCenter(new SuperMap.LonLat(4503.6240321526, -3861.911472192499), 0);
    }

    //数据集泰森多边形
    function bufferAnalystProcess() {
        widgets.alert.clearAlert();
        resultLayer.removeAllFeatures();
        var myThiessenAnalystService = new SuperMap.REST.ThiessenAnalystService(url2),
            dThiessenAnalystParameters = new SuperMap.REST.DatasetThiessenAnalystParameters({
                dataset: "Factory@Changchun"
            });
        myThiessenAnalystService.events.on({
            "processCompleted": thiessenAnalystCompleted,
            "processFailed": thiessenAnalystFailed
        });
        myThiessenAnalystService.processAsync(dThiessenAnalystParameters);
    }

    //几何泰森多边形
    function geometryAnalystProcess() {
        widgets.alert.clearAlert();
        resultLayer.removeAllFeatures();
        //新建几何对象数组
        var points = [
            new SuperMap.Geometry.Point(5238.998556, -1724.229865),
            new SuperMap.Geometry.Point(4996.270055, -2118.538477),
            new SuperMap.Geometry.Point(5450.34263, -2070.794081),
            new SuperMap.Geometry.Point(5317.70775, -2521.162355),
            new SuperMap.Geometry.Point(5741.149405, -1970.130198),
            new SuperMap.Geometry.Point(4716.133098, -1575.858795),
            new SuperMap.Geometry.Point(5447.671615, -2255.928819),
            new SuperMap.Geometry.Point(4783.423507 - 1135.598744),
            new SuperMap.Geometry.Point(5472.712382, -2189.15344),
            new SuperMap.Geometry.Point(5752.716961, -2425.40363)];

        var myThiessenAnalystService = new SuperMap.REST.ThiessenAnalystService(url2, { isInTheSameDomain: true }); //初始化泰森多边形分析服务类
        var gThiessenAnalystParameters = new SuperMap.REST.GeometryThiessenAnalystParameters({points: points});   //初始化泰森多边形分析参数基类
        myThiessenAnalystService.events.on({
            "processCompleted": thiessenAnalystCompleted,
            "processFailed": thiessenAnalystFailed
        });
        //向iserver发送请求
        myThiessenAnalystService.processAsync(gThiessenAnalystParameters);


    }

    //请求成功
    function thiessenAnalystCompleted(serviceEventArgs) {
        resultLayer.removeAllFeatures();
        var feature, polygonFeature, features = [];
        for (var i = 0; i < serviceEventArgs.result.regions.length; i++) {
            feature = serviceEventArgs.result.regions[i];
            polygonFeature = new SuperMap.Feature.Vector(feature);
            features.push(polygonFeature);
        }
        resultLayer.addFeatures(features);
    }

    //请求失败
    function thiessenAnalystFailed(serviceEventArgs) {
        widgets.alert.showAlert(serviceEventArgs.error.errorMsg, false);
    }

    function removeResult() {
        widgets.alert.clearAlert();
        resultLayer.removeAllFeatures();
    }
</script>

</body>
</html>